
package com.yimixian.app.order;

import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.R;
import com.yimixian.app.adapter.ChooseCouponRecyclerAdapter;
import com.yimixian.app.model.ValidatedOrder;
import com.ymx.sdk.widget.recyclerview.ListRecyclerAdapter;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
 
public class SelectCouponFragment extends Fragment {
    RecyclerView mRecyclerView;
    ChooseCouponRecyclerAdapter mAdapter;
    ArrayList<ValidatedOrder.Bonus> mList = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_coupon_list, null);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.lv_coupon);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mList.clear();
        if (isAdded()) {
            mList = getArguments()
                .getParcelableArrayList(SelectCouponActivity.INTENT_EXTRA_COUPON_LIST);
            mAdapter = new ChooseCouponRecyclerAdapter(mList, getActivity());
            mAdapter.setList(mList);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            mRecyclerView.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
        }
        mAdapter.setOnItemClickListener(
                new ListRecyclerAdapter.OnItemClickListener() {
                    @Override
                    public void OnItemClick(View view, int position) {
                        if (!mList.get(position).isUsable()) {
                            return;
                        }
                        mList = mAdapter.setSelectItem(position);
                        mAdapter.setList(mList);
                        mAdapter.notifyDataSetChanged();
                    }
                });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public int getSelectPos() {
        int pos = -1;
        for (int i = 0; i < mAdapter.mList.size(); i++) {
            if (mAdapter.mList.get(i).isChecked()) {
                pos = i;
            }
        }
        return pos;

    }

}
